﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Windows;


namespace SurvivalSolitair
{
    public class Card
    {
        public string Suit { get; private set; }
        public string Value { get; private set; }
        public bool faceUp;
        public Image cardFace;
        public Image cardBack;

        public PictureBox myBox = new PictureBox();

        public bool isDragging;
        public double currentX;
        public double currentY;
        public bool isEnabled;

        public Point startPosition;
        public bool topMost;

        public Control Parent;

        public List<DropZone> gameField;

        public void onClick(object o, EventArgs e)
        {

            //Flip();


        }

        private void onMouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                myBox.Top = myBox.Top + (e.Y - (int)currentY);
                myBox.Left = myBox.Left + (e.X - (int)currentX);
            }
        }
        
        public void onMouseDown(object o, MouseEventArgs e)
        {
            if (faceUp)
            {
                startPosition = new Point((int)currentX, (int)currentY);
                isDragging = true;

                currentX = e.X;
                currentY = e.Y;
            }
            else
                Flip();
        }
        public void onMouseUp(object o, EventArgs e)
        {
            isDragging = false;

            /*
             * if( canDrop)
             *      // nothing drop around at current coordinates
             * else
             *  reset pos
             * 
             * 
             * 
             * 
             */
            
            foreach (DropZone z in gameField)
            {
                if (HitTest(z.hitBoxPosition.X, z.hitBoxPosition.Y))
                {
                    eventDropCard(this, z,new PlaceCardArgs(gameField));
                }

            }
            

            
           
        }
        public bool allowStack()
        {

            return topMost;

        }



        public Card(string card,Control parent,List<DropZone> theField)
        {
            Suit = card.Substring(0, 1);
            Value = card.Substring(1, card.Length - 1);
            faceUp = false;

            cardFace = Image.FromFile(Directory.GetCurrentDirectory() + @"\Data\Sprites\" + Suit+ Value + ".png");
            cardBack = Image.FromFile(Directory.GetCurrentDirectory() + @"\Data\Sprites\" + "br1.png");

            myBox.Width = 75;
            myBox.Height = 109;
            myBox.Click += this.onClick;
            myBox.MouseDown += this.onMouseDown;
            myBox.MouseUp += this.onMouseUp;
            myBox.MouseMove += this.onMouseMove;
            myBox.Visible = false;
            myBox.Parent = parent;
            myBox.Enabled = false;

            Parent = parent;

            MoveCard(50, 50);
            parent.Controls.Add(myBox);
            currentX = 50;
            currentY = 50;
            isDragging = false;
            topMost = false;

            UpdateSprite();

            gameField = theField;
        }


        public void Flip()
        {

            faceUp = !faceUp;
            UpdateSprite();
        }
        public void ToggleEnabled()
        {
            myBox.Enabled = !myBox.Enabled;

        }

        public void UpdateSprite()
        {
            myBox.Image = faceUp ? cardFace : cardBack;
        }

        public void MoveCard(int x, int y)
        {
            myBox.Location = new Point(x, y);

        }
        public void ToggleVisible()
        {
            myBox.Visible = !myBox.Visible;

        }

        public bool HitTest(int x,int y)
        {
            double dx = this.currentX - x;
            double dy = this.currentY - y;
            double dr = Math.Sqrt((dx * dx) + (dy * dy));
            int i_dr = (int)dr;

            if (i_dr < 75)
            {

                return true;
            }
            else
                return false; 
        }

        public delegate void droppedCard(Card origin,DropZone target,PlaceCardArgs e);
        public static event droppedCard eventDropCard;



    }
}
